package cn.com.cetccst.compliancecheck.ui;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.databinding.BindingAdapter;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.RecyclerView;

import cn.com.cetccst.compliancecheck.R;
import cn.com.cetccst.compliancecheck.checker.CheckResult;
import cn.com.cetccst.compliancecheck.checker.CheckerManager;
import cn.com.cetccst.compliancecheck.databinding.AdapterComplianceBinding;
import cn.com.cetccst.datacenter.policy.entity.ComplianceEntity;
import cn.com.cetccst.uicomponents.binding.recyclerview.SimpleDataBindingAdapter;

public class ComplianceAdapter extends SimpleDataBindingAdapter<ComplianceEntity, AdapterComplianceBinding> {

    private final ComplianceFragment.OnFragmentInteractionListener listener;

    public ComplianceAdapter(Context context, ComplianceFragment.OnFragmentInteractionListener onFragmentInteractionListener) {
        super(context, R.layout.adapter_compliance, new DiffUtil.ItemCallback<>() {
            @Override
            public boolean areItemsTheSame(@NonNull ComplianceEntity oldItem, @NonNull ComplianceEntity newItem) {
                return oldItem.getKey().equals(newItem.getKey());
            }

            @Override
            public boolean areContentsTheSame(@NonNull ComplianceEntity oldItem, @NonNull ComplianceEntity newItem) {
                return oldItem.getStatus() == newItem.getStatus()
                        && oldItem.getPunish() == newItem.getPunish();
            }
        });

        listener = onFragmentInteractionListener;

        setOnItemClickListener((item, position) -> listener.onListFragmentInteraction(item));
    }

    @Override
    protected void onBindItem(AdapterComplianceBinding binding, ComplianceEntity item, RecyclerView.ViewHolder holder) {
        Shader shader = binding.getShader();
        binding.setEntity(item);
        if (shader == null) {
            binding.setShader(new Shader());
        }
    }

    public class Shader {

        public int getColor(ComplianceEntity entity) {
            if (entity.getStatus() == CheckResult.Pass.getStatus())
                return context.getColor(R.color.compliance_pass_color);
            else
                return context.getColor(R.color.compliance_violation_color);
        }

        public int getImageRes(ComplianceEntity entity) {
            if (entity.getStatus() == CheckResult.Pass.getStatus())
                return R.drawable.compliance_check_yes;
            else
                return R.drawable.compliance_check_no;
        }

        public String getName(ComplianceEntity entity) {
            return CheckerManager.getInstance().getCheckerName(entity.getKey());
        }
    }

    @BindingAdapter(value = {"tintColor", "imgRes"})
    public static void complianceStatus(ImageView view, int color, int imgRes) {
        view.setImageResource(imgRes);
        Drawable drawable = view.getDrawable();
        drawable.setTint(color);
    }
}
